package com.example.lj.uav.view;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.lj.uav.MyApplication;
import com.example.lj.uav.R;
import com.example.lj.uav.view.base.BaseActivity;

import dji.common.error.DJIError;
import dji.common.useraccount.UserAccountState;
import dji.common.util.CommonCallbacks;
import dji.sdk.useraccount.UserAccountManager;

import static com.example.lj.uav.MyApplication.mPref;

public class LoginActivity extends BaseActivity {
    private RelativeLayout login;
    private UserAccountManager accountManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        backToExit();
        accountManager = UserAccountManager.getInstance();
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initView() {
        login = findViewById(R.id.login_rl_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        login();
    }

    private void login() {
        accountManager.logIntoDJIUserAccount(this,
                new CommonCallbacks.CompletionCallbackWith<UserAccountState>() {
                    @Override
                    public void onSuccess(final UserAccountState userAccountState) {
                        showToast("Login Success");
                        jumpToActivityDelay(0);
                    }

                    @Override
                    public void onFailure(DJIError error) {
                        showToast("Login Error:"
                                + error.getDescription());
                    }
                });
    }

    private void jumpToActivityDelay(int time) {
        login.postDelayed(new Runnable() {
            @Override
            public void run() {
                accountManager.getLoggedInDJIUserAccountName(new CommonCallbacks.CompletionCallbackWith<String>() {
                    @Override
                    public void onSuccess(String s) {
                        MyApplication.accountName = s;
                        mPref = getSharedPreferences(s, MODE_PRIVATE);
                        if (!mPref.getBoolean("is_authority1_commit", false)) {
                            toAct(AuthorityActivity.class, true);
                        } else if (!mPref.getBoolean("is_authority2_commit", false)) {
                            toAct(Authority_Activity2.class, true);
                        } else {
                            toAct(MainActivity.class, true);
                        }
                    }

                    @Override
                    public void onFailure(DJIError djiError) {
                        showToast(djiError.getDescription());
                    }
                });
            }
        }, time);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
}
